package com.banmao.desktoptools.copyfilesScene.controller;

import com.banmao.desktoptools.FXMain;
import com.banmao.desktoptools.copyfilesScene.dao.CopyfilesDao;
//import com.banmao.desktoptools.copyfilesScene.service.CopyfilesService;
import com.banmao.desktoptools.copyfilesScene.vo.UserPojo;
import com.banmao.desktoptools.util.SpringContextUtil;
//import com.baomidou.mybatisplus.core.conditions.Wrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.ResourceBundle;

@FXMLController
public class CopyFilesController implements Initializable {

    @FXML
    private Text helloText;

    @FXML
    private Button helloButton;

    private Boolean flag = true;

    //@Autowired
    //private CopyfilesService copyfilesService;

    private JdbcTemplate jdbcTemplate;
    private CopyfilesDao copyfilesDao;

    @FXML
    void changeText(ActionEvent event) {
        String user_name = "null";
        if(jdbcTemplate != null) {
            System.out.println("jdbcTemplate");
            List<Map<String, Object>> data = jdbcTemplate.queryForList("select * from wx_notebook.user_info ui ");
            if(data.size() > 0) {
                Map<String, Object> stringObjectMap = data.get(new Random().nextInt(data.size() - 1));
                user_name = (String) stringObjectMap.get("user_name");
            }
        }
        if(copyfilesDao != null) {
            System.out.println("copyfilesDao");
            //QueryWrapper<UserPojo> objectQueryWrapper = new QueryWrapper<>();
            //objectQueryWrapper.select("user_name");
            //List<Map<String, Object>> data = copyfilesDao.selectMaps(objectQueryWrapper);
            List<UserPojo> data = copyfilesDao.getAllUser();
            if(data.size() > 0) {
                UserPojo userPojo = data.get(new Random().nextInt(data.size() - 1));
                user_name = (String) userPojo.getUser_name();
            }
        }


        String path = "";
        //DirectoryChooser directoryChooser = new DirectoryChooser();
        //File file = directoryChooser.showDialog(FXMain.getStage());
        //if(file != null) {
        //    path = file.getAbsolutePath();
        //}

        if(flag) {
            helloText.setText("世界" + user_name + "\n" + path);
        } else {
            helloText.setText("你好" + user_name + "\n" + path);
        }
        flag = !flag;


    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            jdbcTemplate = SpringContextUtil.getBean(JdbcTemplate.class);
        } catch (BeansException e) {
            System.out.println("未获取到Bean jdbcTemplate");
        }
        try {
            copyfilesDao = SpringContextUtil.getBean(CopyfilesDao.class);
        } catch (BeansException e) {
            System.out.println("未获取到Bean copyfilesDao");
        }
    }
}
